<template>
  <div class="page1">

    <div style="display: flex;justify-content: flex-start">
      <el-button type="primary" @click="insert">新增</el-button>
    </div>


    <el-table :data="arr" stripe style="width: 100%">
      <el-table-column prop="name" label="姓名" width="180"/>

      <el-table-column prop="age" label="年龄" width="180"/>

      <el-table-column prop="sex" label="性别"/>

      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-button @click="handleClick(scope.row)" type="text" size="small">查看</el-button>
          <el-button type="text" size="small" @click="Showdialog(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="dial-header">
      <el-dialog :title="title" :visible.sync="showFlag" style="justify-content: center">
        <el-form :model="form">
          <el-form-item label="姓名" :label-width="formLabelWidth">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item label="性别" :label-width="formLabelWidth">
            <div style="display: flex;justify-content: flex-start">
              <el-select v-model="form.sex" placeholder="请选择性别">
                <el-option label="男" value="男"></el-option>
                <el-option label="女" value="女"></el-option>
              </el-select>
            </div>
          </el-form-item>

          <el-form-item label="年龄" :label-width="formLabelWidth">
            <el-input v-model="form.age" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
           
        <div slot="footer" class="dialog-footer">
          <el-button @click="showFlag = false">取 消</el-button>
          <el-button type="primary" @click="update">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <div class="block" style="display: flex;justify-content: flex-end;margin-top: 20px">
      <el-pagination
        layout="prev, pager, next"
        @current-change="handleCurrentChange"
        :page-size="3"
        :total="count">
      </el-pagination>
    </div>
  </div>

</template>

<style>
  .page1 {
    display: flex;
    flex: 1;
    justify-content: flex-start;
    flex-direction: column;
    width: 1000px;
  }

</style>


<script>

  import axios from 'axios'
  import index from "../router";

  export default {
    data() {
      return {
        arr: [],
        showFlag: false,
        radio: "",
        form: {
          _id: null,
          name: null,
          age: null,
          sex: null
        },
        formLabelWidth: '120px',
        count: 0,
        title: '',
        number: 1
      }
    },
    methods: {
      handleClick(r) {

        this.arr.forEach(function (item, index) {
          console.log(index + "----->" + item.name)
        })


      },
      Showdialog(r) {
        this.showFlag = true;

        let that = this
        this.title = "修改信息"
        axios.get('/api/userByid', {
          params: {
            id: r._id
          }
        }).then(function (response) {
          that.form = response.data
        }).catch(function (error) {
          console.log(error);
        });

      },

      //修改 个人信息
      update() {
        console.log(this.form)
        let that = this
        axios.post('/api/update', that.form).then(function (response) {
          console.log(response);
          that.showFlag = false

          if (that.form._id == null) {
            that.getAllUsers()
            that.getCount();
          } else {
            that.arr.forEach(function (item, index) {
              if (item._id === that.form._id) {
                item.name = that.form.name;
                item.age = that.form.age;
                item.sex = that.form.sex
              }
            })
          }
        }).catch(function (error) {
          console.log(error)
        })
      },
      handleCurrentChange(number) {
        console.log(number)
        this.number = number
        this.getAllUsers()
      },
      getAllUsers() {
        let that = this;
        axios.get('/api/findAllByPage', {
          params: {
            page: that.number - 1,
            size: 3
          }
        }).then(function (response) {
          that.arr = response.data
        }).catch(function (error) {
          console.log(error);
        });
      },
      getCount() {
        let that = this
        axios.get('api/count').then(function (response) {
          that.count = response.data
        })
      },
      insert() {
        this.showFlag = true;
        this.title = "新增信息"
      }
    },
    created: function () {
      this.getAllUsers();
      this.getCount();
    }
  }
</script>
